package com.leyou.page.service;


import com.leyou.item.api.SpecificationApi;
import com.leyou.item.pojo.*;
import com.leyou.page.clients.BrandClient;
import com.leyou.page.clients.CategoryClient;
import com.leyou.page.clients.GoodsClient;
import com.leyou.page.clients.SpecificationClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsService {


    @Autowired
    private BrandClient brandClient;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecificationClient specificationClient;


    static final Logger logger = LoggerFactory.getLogger(GoodsService.class);


    public Map<String, Object> loadModel(Long id) {

        try {
            //1.创建模型数据
            Map<String, Object> modelMap = new HashMap<>();

            //2 查询各种数据

            //2.1查询spu
            Spu spu = this.goodsClient.querySpuById(id);

            //2.2查询spuDetail
            SpuDetail spuDetail = this.goodsClient.querySpuDetailById(id);

            //2.3查询sku
            List<Sku> skus = this.goodsClient.querySkuBySpuId(id);

            //3.装填数据模型

            modelMap.put("spu", spu);
            modelMap.put("spuDetail", spuDetail);
            modelMap.put("skus", skus);

            //4.准备商品分类

            List<Category> categories = getCategories(spu);

            if (categories != null) {
                modelMap.put("categories", categories);
            }

            //5.准备品牌分类
            List<Brand> brands = this.brandClient.queryBrandByIds(Arrays.asList(spu.getBrandId()));
            modelMap.put("brands", brands.get(0));

            //6.查询规格组内参数及数据
            List<SpecGroup> groups = this.specificationClient.querySpecsByCid(spu.getCid3());
            modelMap.put("groups", groups);

            //7.查询分类下的特有规格参数
            List<SpecParam> params = this.specificationClient.querySpecParam(null, spu.getCid3(), null);

            //7.1把取出来的数据拼接成 id：name格式的键值对形式

            Map<Long, String> paramMap = new HashMap<>();

            params.forEach(param -> {
                paramMap.put(param.getId(), param.getName());
            });

            modelMap.put("paramMap",paramMap);

            return modelMap;
        } catch (Exception e) {
            logger.error("加载商品分类出错,spuId：{}",id,e);
        }
        return null;
    }

    private List<Category> getCategories(Spu spu) {

        try {
            List<String> names = this.categoryClient.queryNameByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));

            Category c1 = new Category();
            c1.setName(names.get(0));
            c1.setId(spu.getCid1());

            Category c2 = new Category();
            c2.setName(names.get(1));
            c2.setId(spu.getCid2());

            Category c3 = new Category();
            c3.setName(names.get(2));
            c3.setId(spu.getCid3());

            return Arrays.asList(c1, c2, c3);
        } catch (Exception e) {
            logger.error("查询商品分类出错，spuId：{}", spu.getId(), e);
        }
        return null;
    }


}
